/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javafx.scene.Group;
import model.Player;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.control.Label;
import vinciappli.Templates;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.geometry.HPos;
import javafx.scene.layout.LayoutInfo;
import javafx.geometry.VPos;
import model.Civilization;
import javafx.animation.transition.SequentialTransition;
import javafx.animation.Timeline;
import javafx.animation.transition.PauseTransition;
import control.ControllerPrincipal;
import javafx.scene.control.OverrunStyle;

/**
 * @author Sik
 */
 public-read var colors: Color[] = [Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.VIOLET, Color.CHOCOLATE];

public class PlayerSlot extends Group
{
    public var p: Player;
    public var pts: Integer = p.getPoints();
    public var availableTroops : Integer = p.getAvailableTroops();
    public var nbTroops : Integer = p.getNbTroops();
    var cards : String[] = ["","","",""];
    public-read var height: Integer = Templates.PLAYER_SLOT_HEIGHT;
    public-read var width: Integer = Templates.PLAYER_SLOT_WIDTH;
    public-read var isActive : Boolean = false;
    
    var myFont: Font =
            Font
            {
                name: "Impact"
                size: 14
            }

    var pseudo : Label =
    Label
    {
        text: p.getNickname()
        font: Font
        {
            name: "Impact"
            size: 24
        }
        layoutInfo: LayoutInfo
        {
            width: 85
        }
        hpos: HPos.CENTER
        textWrap: false
        textOverrun: OverrunStyle.ELLIPSES
    }

    var border : Rectangle =
    Rectangle
    {
        height: height
        width: width - 3
        fill: null
        stroke: Color.BLACK
        strokeWidth: 6
    }

    var seqTransition =
    SequentialTransition
    {
        repeatCount: Timeline.INDEFINITE
        content:
        [
            PauseTransition
            {
                duration: 0.7s
                action: function():Void
                {
                    border.stroke = Color.GOLD;
                }
            },
            PauseTransition
            {
                duration: 0.7s
                action: function():Void
                {
                    border.stroke = Color.BLACK;
                }
            }
        ]
    }

    public function updateDisplay() : Void
    {
        this.pts = p.getPoints();
        this.availableTroops = p.getAvailableTroops();
        this.nbTroops = p.getNbTroops();
    }

    public function setCivilization(depressed : Boolean, civ : Civilization)
    {
        if(depressed)
        {
            if(civ.getCaracteristic1() == null)
            {
                cards[2] = "file:///{{Templates.CARD_PATH}}{{Templates.EMPTY_CARD}}{{Templates.IMG_EXTENSION}}";
            }
            else
            {
                cards[2] = "file:///{{Templates.CARD_PATH}}{{civ.getCaracteristic1().getType().getType()}}{{Templates.IMG_EXTENSION}}";
            }
            if(civ.getCaracteristic2() == null)
            {
                cards[3] = "file:///{{Templates.CARD_PATH}}{{Templates.EMPTY_CARD}}{{Templates.IMG_EXTENSION}}";
            }
            else
            {
                cards[3] = "file:///{{Templates.CARD_PATH}}{{civ.getCaracteristic2().getType().getType()}}{{Templates.IMG_EXTENSION}}";
            }
        }
        else
        {
            cards[0] = "file:///{{Templates.CARD_PATH}}{{civ.getCaracteristic1().getType().getType()}}{{Templates.IMG_EXTENSION}}";
            cards[1] = "file:///{{Templates.CARD_PATH}}{{civ.getCaracteristic2().getType().getType()}}{{Templates.IMG_EXTENSION}}";
        }
    }



    public function setActive(activated : Boolean) : Void
    {
        if(activated)
        {
            this.toFront();
            seqTransition.play();
            isActive = true;
        }
        else
        {
            seqTransition.stop();
            this.border.stroke = Color.BLACK;
            isActive = false;
        }
    }

    init
    {

        content =
                {
                    Group
                    {
                        content: [
                            ImageView
                            {
                                fitHeight: height
                                fitWidth: width
                                image: Image
                                {
                                    url: "file:///{{Templates.GUI_PATH}}bgpapier.jpg"
                                }
                            }

                            border,

                            VBox
                            {
                                translateX: 5
                                content: [
                                    HBox
                                    {
                                        content: [
                                            Group
                                            {
                                                translateY: 5
                                                content: [
                                                    ImageView
                                                    {
                                                        fitWidth: 85
                                                        preserveRatio: true
                                                        image: Image
                                                        {
                                                            url: p.getUrlAvatar()
                                                        }
                                                    }

                                                    Rectangle
                                                    {
                                                        height: 85
                                                        width: 85
                                                        fill: null
                                                        stroke: colors[ControllerPrincipal.getINSTANCE().getControllerPlayer().getListPlayers().indexOf(p)]
                                                        strokeWidth: 3
                                                        strokeLineCap: StrokeLineCap.ROUND
                                                    }
                                                ]
                                            }

                                            VBox
                                            {
                                                content: [
                                                    pseudo,

                                                    Label
                                                    {
                                                        text: "Score"
                                                        font: Font
                                                        {
                                                            name: "Impact"
                                                            size: 12
                                                        }
                                                        layoutInfo: LayoutInfo
                                                        {
                                                            width: 85
                                                        }
                                                        hpos: HPos.CENTER
                                                    }

                                                    Label
                                                    {
                                                        text: bind String.valueOf(pts)
                                                        textFill: Color.BLUE
                                                        font: Font
                                                        {
                                                            name: "Impact"
                                                            size: 30
                                                        }
                                                        layoutInfo: LayoutInfo
                                                        {
                                                            width: 85
                                                        }
                                                        hpos: HPos.CENTER
                                                    }
                                                ]
                                            }
                                        ]
                                    }

                                    Text
                                    {
                                        translateY: 3
                                        content: bind "Troupes dispo/total : {
                                        {
                                            availableTroops
                                        }} / {
                                        {
                                            nbTroops
                                        }}"
                                        font: myFont
                                    }

                                    VBox
                                    {
                                        translateY: 3
                                        content: [
                                            Group
                                            {
                                                content: [
                                                    Rectangle
                                                    {
                                                        height: 65
                                                        width: 180
                                                        fill: null
                                                        stroke: Color.WHITE
                                                        strokeWidth: 4
                                                    }

                                                    Rectangle
                                                    {
                                                        translateY: 72
                                                        height: 65
                                                        width: 180
                                                        fill: null
                                                        stroke: Color.WHITE
                                                        strokeWidth: 4
                                                    }

                                                    VBox
                                                    {
                                                        content: [
                                                            HBox
                                                            {
                                                                translateX: 15
                                                                translateY: 5
                                                                spacing: 5
                                                                content: [
                                                                    Label
                                                                    {
                                                                        text: "ACTIF"
                                                                        font: Font
                                                                        {
                                                                            name: "Impact"
                                                                            size: 14
                                                                        }
                                                                        layoutInfo: LayoutInfo
                                                                        {
                                                                            height: 60
                                                                        }
                                                                        vpos: VPos.CENTER
                                                                    }

                                                                    ImageView
                                                                    {
                                                                        fitHeight: 55
                                                                        preserveRatio: true
                                                                        image: bind Image
                                                                        {
                                                                            url : cards[0]
                                                                            //url: "file:///{{Templates.CARD_PATH}}agriculture.jpg"
//                                                                            url : "{{Templates.CARD_PATH}}{{p.getEmpire(false).getCivilization().getCaracteristic1()}}.png"
                                                                        }
                                                                    }

                                                                    ImageView
                                                                    {
                                                                        fitHeight: 55
                                                                        preserveRatio: true
                                                                        image: bind Image
                                                                        {
                                                                            url : cards[1]
                                                                            //url: "file:///{{Templates.CARD_PATH}}agriculture.jpg"
                                                                            //url : "{{Templates.CARD_PATH}}{{p.getEmpire(false).getCivilization().getCaracteristic2()}}.png"
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                    
                                                            HBox
                                                            {
                                                                translateX: 9
                                                                translateY: 18
                                                                spacing: 5
                                                                content: [
                                                                    Label
                                                                    {
                                                                        text: "DECLIN"
                                                                        font: Font
                                                                        {
                                                                            name: "Impact"
                                                                            size: 14
                                                                        }
                                                                        layoutInfo: LayoutInfo
                                                                        {
                                                                            height: 60
                                                                        }
                                                                        vpos: VPos.CENTER
                                                                    }

                                                                    ImageView
                                                                    {
                                                                        fitHeight: 55
                                                                        preserveRatio: true
                                                                        image: bind Image
                                                                        {
                                                                            url : cards[2]
                                                                            //url: "file:///{{Templates.CARD_PATH}}agriculture.jpg"
                                                                        //url : "{{Templates.CARD_PATH}}{{p.getEmpire(true).getCivilization().getCaracteristic1()}}.png"
                                                                        }
                                                                    }

                                                                    ImageView
                                                                    {
                                                                        fitHeight: 55
                                                                        preserveRatio: true
                                                                        image: bind Image
                                                                        {
                                                                            url : cards[3]
                                                                           // url: "file:///{{Templates.CARD_PATH}}agriculture.jpg"
                                                                        //url : "{{Templates.CARD_PATH}}{{p.getEmpire(true).getCivilization().getCaracteristic2()}}.png"
                                                                        }
                                                                    }
                                                                ]
                                                            }
                                                        ]
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
    }
